package converters;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import daos.DAOturma;
import entidades.Turma;

@FacesConverter(value="turmaConverter", forClass=Turma.class)
public class TurmaConverter implements Converter{
	public List<Turma> turmas;
	DAOturma daoTurma = new DAOturma();

	public TurmaConverter(){
		turmas = daoTurma.listar();
	}
	 
	public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {  
		if (submittedValue.trim().equals("")) {  
			return null;
		} 
		else{ 
			for (Turma turma : turmas) {  
				if (String.valueOf(turma.getCodTurma()).equals(submittedValue)) {
					return turma;
				}
			}
		}
		return null;
	}

	public String getAsString(FacesContext facesContext, UIComponent component, Object value) {  
		if (value == null || value.equals("")) {  
			
			return "";  
		} 
		else {  
			return String.valueOf(((Turma) value).getCodTurma());  
		}  
	}
}